package com.elegant.code.spi.web.gateway.plugins.divide;

import com.elegant.code.spi.web.gateway.ServicesHolder;

import java.util.concurrent.ThreadLocalRandom;

public class RandomLoadBalance implements LoadBalance{
    @Override
    public String load(String serviceName) {
        int addressCacheSize = ServicesHolder.addressPathSize(serviceName);
        String address = null;
        if (addressCacheSize > 0) {
            if (addressCacheSize == 1) {
                address = ServicesHolder.getAddress(serviceName, 0);
            } else {
                address = ServicesHolder.getAddress(serviceName, ThreadLocalRandom.current().nextInt(addressCacheSize));
            }
            System.out.println(">>>get only address node:" + address);

            //从zk服务注册中心获取某个服务地址
        }
        return address;
    }
}
